# Author: lcy

import requests
import json
from PIL import Image
from io import BytesIO

# 地理编码、逆地理编码
# 地理编码

def geocodes(key,address,city=None,batch="false",sig=None,output='json',callback=None):
    """获取地理编码"""
    url = 'https://restapi.amap.com/v3/geocode/geo?parameters'
    params={
        'key': key,
        'address':address,
        'city':city,
        'batch':batch,
        'sig':sig,       
    }
    r = requests.get(url,params=params)
    r.json()
    return  r.json()


# 逆地理编码


def regeo(key,location,poitype=None,radius=1000,extensions="base",batch="false",roadlevel=1,sig=None,output="json",callback=None,homeorcorp=0):
    """获取逆地理编码"""
    url = 'https://restapi.amap.com/v3/geocode/regeo?parameters'
    params = {"key": key,
              "location": location,
              "poitype": poitype,
              "radius": radius,
              "extensions": extensions,
              "batch": batch,
              "roadlevel":roadlevel,
              "sig": sig,
              "output": output,
              "callback": callback,
              "homeorcorp": homeorcorp
             }
    r = requests.get(url, params=params)
    return r.json()



# 路径规划
# 步行规划


def walking(key,origin,destination,sig=None,output="json",callback=None):
    """可以规划100KM以内的步行通勤方案，并且返回通勤方案的数据"""
    url = "https://restapi.amap.com/v3/direction/walking?parameters"
    params = {
        "key":key,
        "origin": origin,
        "destination": destination,
        "sig": sig,
        "output": output,
        "callback": callback
    }
    r = requests.get(url, params=params)
    return r.json()


# 公交路径规划


def integrated(key,origin,destination,city,cityd=None,extensions='base',strategy=None,nightflag=0,date=None,time=None,sig=None)->dict:
    """规划综合各类公共（火车、公交、地铁）交通方式的通勤方案，并且返回通勤方案的数据"""
    url = 'https://restapi.amap.com/v3/direction/transit/integrated?parameters'
    params={
        'key':key,
        'origin':origin,
        'destination':destination,
        'city':city,
        'cityd':cityd,
        'extensions':extensions,
        'strategy':strategy,
        'nightflag':nightflag,
        'date':date,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data


# 行政区域查询


def Enquiries(key,keywords,subdistric,page=1,offset=20,extensions="base",district_filter="adcode",callback=None,output="json"):
    """根据用户输入的搜索条件可以帮助用户快速的查找特定的行政区域信息"""
    url = "https://restapi.amap.com/v3/config/district?parameters"
    params = {
        "key": key,
        "keywords": keywords,
        "subdistric": subdistric,
        "page": page,
        "offset": offset,
        "extensions": extensions,
        "filter": district_filter,
        "callback": callback,
        "output": output
    }
    r = requests.get(url, params=params)
    return r.json()


# 搜索POI
# 关键字搜索


def poikw(key,keywords,types=None,city=None,citylimit="false",children=0,offset=20,page=1,extensions="base",sig=None,output="json",callback=None):
    """通过用POI的关键字进行条件搜索"""
    url = 'https://restapi.amap.com/v3/place/text?parameters'
    params={
        "key": key,
        "keywords": keywords,
        "types": types,
        "city": city,
        "citylimit": citylimit,
        "children": children,
        "offset": offset,
        "page": page,
        "extensions": extensions,
        "sig": sig,
        "output": output,
        "callback": callback
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data


# 周边搜索


def poi_around(key,location,keywords=None,types="050000|070000|120000",city=None,radius=3000,sortrule="distance",offset=20,page=1,extensions="base",sig=None,output="json",callback=None):
    """在用户传入经纬度坐标点附近，在设定的范围内，按照关键字或POI类型搜索"""
    url = 'https://restapi.amap.com/v3/place/around?parameters'
    params = {
        "key": key,
        "location": location,
        "keywords": keywords,
        "types": types,
        "city": city,
        "radius": radius,
        "sortrule": sortrule,
        "offset": offset,
        "page": page,
        "extensions": extensions,
        "sig": sig,
        "output": output,
        "callback": callback
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data

# 多边形搜索
def polygons(key,polygon,keywords=None):
    """多边形搜索的自定义函数"""
    url_poi="https://restapi.amap.com/v3/place/polygon?parameters "
    params={
        "key":key,
        "polygon":polygon,
        "keywords":keywords
    }
    response = requests.get(url_poi,params=params)
    return response.json()

# IP定位


def ip(key,ip,sig=None,output="json"):
    """根据用户输入的IP地址，能够快速的帮用户定位IP的所在位置"""
    url = 'https://restapi.amap.com/v3/ip?parameters'
    params = {
        "key": key,
        "ip": ip,
        "sig": sig,
        "output": output
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data

# 批量请求接口 
def reqs():
    url="https://restapi.amap.com/v3/batch?key=<eaa6c638561cbb9629a98d39b44f7261>"
    params={
    "ops": [{"url": "/v3/place/around?offset=10&page=1&key=<eaa6c638561cbb9629a98d39b44f7261>&location=116.50394379585519,39.278209477408794&output=json&radius=100000&types=080000"
        },
        {"url": "/v3/place/around?offset=10&page=1&key=<eaa6c638561cbb9629a98d39b44f7261>&location=118.50394379585519,39.278209477408794&output=json&radius=100000&types=080000"
        }
        ]
        }
    response=requests.get(url,params=params)
    r = response.json()
    return r

# 静态地图


def staticmap(key,location,zoom=15,size="400*400",scale=1,markers=None,labels=None,paths=None,traffic=0,sig=None):
    """静态地图服务通过返回一张地图图片响应HTTP请求，使用户能够将高德地图以图片形式嵌入自己的网页中。用户可以指定请求的地图位置、图片大小、以及在地图上添加覆盖物，如标签、标注、折线、多边形。"""
    from PIL import Image
    from io import BytesIO
    staticmap_url = "https://restapi.amap.com/v3/staticmap?parameters"
    params = {
        "key": key,
        "location": location,
        "zoom": zoom,
        "size": size,
        "scale": scale,
        "markers": markers,
        "labels": labels,
        "paths": paths,
        "traffic": traffic,
        "sig": sig
    }
    r = requests.get(staticmap_url, params)
    data = Image.open(BytesIO(r.content))
    return data


# 坐标转换


def convert(key,location,coordsys="autonavi",sig=None,output="json"):
    """将用户输入的非高德坐标（GPS坐标、mapbar坐标、baidu坐标）转换成高德坐标"""
    convert_url = "https://restapi.amap.com/v3/assistant/coordinate/convert?parameters"
    params = {
        "key": key,
        "locations": location,
        "coordsys": coordsys,
        "sig": sig,
        "output": output}
    r = requests.get(convert_url, params)
    return r.json()


# 天气查询


def weather(key,city,extension="all",output="json"):
    """根据用户输入的adcode，查询目标区域当前/未来的天气情况"""
    weather_url = "https://restapi.amap.com/v3/weather/weatherInfo?parameters"
    params = {
        "key": key,
        "city": city,
        "extensions":extension,
        "output": output
    }
    r = requests.get(weather_url, params)
    return r.json()


# 输入提示


def input_tips(key,keyword,poi_type=None,location=None,city=None,citylimit="false",datatype="all",sig=None,output="json",callback=None):
    """提供根据用户输入的关键词查询返回建议列表"""
    input_tips_url = "https://restapi.amap.com/v3/assistant/inputtips?parameters"
    params = {
        "key": key,
        "keywords": keyword,
        "type": poi_type,
        "location": location,
        "city": city,
        "citylimit": citylimit,
        "datatype": datatype,
        "sig": sig,
        "output": output,
        "callback": callback
    }
    r = requests.get(input_tips_url, params)
    return r.json()


# 地理围栏


def creat_geofence():
    creat_geofence_url = 'https://restapi.amap.com/v4/geofence/meta?key={}'
    params = {
        "name":"测试围栏",
        "points":"116.670040,23.468340;116.669649,23.466642;116.671216,23.466342;116.672627,23.468448;",
        "enable":"true",
        "repeat": "Mon,Tues,Wed,Thur,Fri,Sat,Sun",
    }
    r = requests.post(creat_geofence_url.format(key),json=params)
    return r.json()

